//
//  RCSBaseViewController.m
//  RCS
//
//  Created by wFeng on 14-8-11.
//  Copyright (c) 2014年 Chinasofti. All rights reserved.
//

#import "ASBaseViewController.h"
//#import "RcSFileUtil.h"
//#import "UIImage+Extend.h"
//#import <objc/runtime.h>

@interface ASBaseViewController ()
{
    UILabel* _naviTitleLable;
}

@end

@implementation ASBaseViewController

- (void)viewDidLoad
{
    //_sUserId = DcN_APP_UserId;
    [super viewDidLoad];
    
    self.view.backgroundColor = RGB(237, 238, 239);
    
    if (SYSTEM_VERSION >=7.0)
	{
        self.edgesForExtendedLayout = UIRectEdgeNone;
        UINavigationBar* bar = [UINavigationBar appearance];
        //[bar setBarTintColor:[UIColor clearColor]];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
	NSLog(@"内存不足:%@",[self class]);
}

- (void)dealloc
{
    [self removeObserver];
}

- (void)addObserver
{
//    id<IRcSCallbackObserverCenter> center = rcsApiGetCallbackObserverCenter();
//    [center addObserver:self withType:UI_SKIN_UPDATE];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(handleUIApplicationWillChangeStatusBarFrameNotification:)
                                                 name:UIApplicationDidEnterBackgroundNotification
                                               object:nil];//监听系统状态栏的变化
}

- (void)removeObserver
{
//    id<IRcSCallbackObserverCenter> center = rcsApiGetCallbackObserverCenter();
//    [center delObserver:self withType:UI_SKIN_UPDATE];
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewWillAppear:(BOOL)animated
{
    [self reloadSkinImage];
    //非根视图默认添加返回按钮
    if ([self.navigationController.viewControllers count] > 0
        && self != [self.navigationController.viewControllers objectAtIndex:0])
    {
        [self setLeftButtonWithImageName:@"icon-3" bgImageName:nil];
    }
    
    if (SYSTEM_VERSION >= 7.0f) {
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    }
    
    [super viewWillAppear:animated];
}

- (void)handleUIApplicationWillChangeStatusBarFrameNotification:(NSNotification*)notification
{
    
}

- (void)setNavTitleColor:(UIColor*)titleColor
{
    _naviTitleLable.textColor = titleColor;

    [self.view setNeedsDisplay];
}

- (void)setNavTitleFontSize
{
    _naviTitleLable.font = [UIFont boldSystemFontOfSize:16];
}

 
//设置navbar的标题
- (void)setStrNavTitle:(NSString *)navTitle
{
    _naviFrame = CGRectMake(0.f, StatusbarSize, 320, 44.f);

    _strNavTitle = navTitle;
    UIView* navTitleView = (self.navigationItem.titleView);

    if([navTitleView isKindOfClass:[UILabel class]])//先要判断是否为label
    {
        _naviTitleLable = (UILabel*)navTitleView;
        _naviTitleLable.text = navTitle;
    }
    else
    {
        _naviTitleLable = [[UILabel alloc] initWithFrame:CGRectZero];
        _naviTitleLable.text= navTitle;
        self.navigationItem.titleView = _naviTitleLable;
    }
    
    _naviTitleLable.backgroundColor = [UIColor clearColor];
    _naviTitleLable.textAlignment = NSTextAlignmentCenter;
    _naviTitleLable.font = [UIFont boldSystemFontOfSize: 16.0];//[RCSFontManage fontWithName: RCSFontDefault];
    _naviTitleLable.textColor = RGB(102, 102, 102);//[RCSFontManage tintColor: RCSFontDefault];//[UIColor blackColor];
    [_naviTitleLable sizeToFit];
}

- (void)setNavTitleName:(NSString*)name number:(NSInteger)number
{
    NSString* numberStr = [NSString stringWithFormat:@"(%ld)",(long)number];
    //NSString*defaultStr = @"群讨论组标群讨论组标";
    NSString* defaultStr = @"群讨论组标群讨论";
    NSString* nameStr = name;
//    CGSize si = [defaultStr sizeWithFont:[UIFont boldSystemFontOfSize: 20.0] constrainedToSize:CGSizeMake(CGFLOAT_MAX, 44) lineBreakMode:NSLineBreakByCharWrapping];
    NSString* title = nil;
    if (nameStr.length > defaultStr.length)
    {
		//此处应该增加群组人数的判断，8470问题单，当群组人数是两位数字的时候则群组title显示不全
	    if (numberStr.length > 3) // eg: (4)
		{
		    NSUInteger subLength = defaultStr.length - (numberStr.length - 3);
		    nameStr = [name substringToIndex:subLength];
	    }
	    else
		{
		    nameStr = [name substringToIndex:defaultStr.length];
		}
		//nameStr = [name substringToIndex:defaultStr.length];
        title = [NSString stringWithFormat:@"%@...%@",nameStr,numberStr];
    }
    else
    {
       title = [NSString stringWithFormat:@"%@%@",nameStr,numberStr];
    }
	
    [self setStrNavTitle:title];
}

#pragma mark -----------左右按钮---------------
- (void)setRightText:(NSString*)text{
    
    [self createLeftAndRightMenuItem:^UIView *(KNaviItemIndex nIndex) {
        UIButton* button = nil;
        if (nIndex == KRightButtonItem) {
			NSInteger width = [text length] == 4 ? NAVBUTTON_WIDTH * 2 : NAVBUTTON_WIDTH;
            button = [UIButton buttonWithType:UIButtonTypeCustom];
            [button setFrame:CGRectMake(0, BUTTONMarginUP, width, NAVBUTTON_HEIGHT)];
            [button setTitle:text forState:UIControlStateNormal];
            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [button addTarget:self action:@selector(rightButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
        }
        
        return button;
    }];
}

- (void)setRightTextBlackList:(NSString*)text
{
    [self createLeftAndRightMenuItem:^UIView *(KNaviItemIndex nIndex) {
        UIButton* button = nil;
        if (nIndex == KRightButtonItem) {
            button = [UIButton buttonWithType:UIButtonTypeCustom];
            [button setFrame:CGRectMake(0, BUTTONMarginUP, NAVBUTTONTEXT_WIDTH, NAVBUTTON_HEIGHT)];
            [button setTitle:text forState:UIControlStateNormal];
            button.titleLabel.font = [UIFont systemFontOfSize:14];
            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [button addTarget:self action:@selector(rightButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
        }
        
        return button;
    }];
}

- (void)setLeftText:(NSString*)text
{
    [self createLeftAndRightMenuItem:^UIView *(KNaviItemIndex nIndex) {
        UIButton* button = nil;
        if (nIndex == KLeftButtonItem) {
            button = [UIButton buttonWithType:UIButtonTypeCustom];
            [button setFrame:CGRectMake(0, BUTTONMarginUP, NAVBUTTON_WIDTH, NAVBUTTON_HEIGHT)];
            [button setTitle:text forState:UIControlStateNormal];
            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [button addTarget:self action:@selector(leftButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
            self.leftButton = button;
        }
        
        return button;
    }];
}

#pragma mark -----------左右按钮---------------
//设置导航左边的button的图片名和背景图片名
- (void)setLeftButtonWithImageName:(NSString *)imageName bgImageName:(NSString *)bgImageName
{
    UIButton *tmpLeftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    
    tmpLeftButton.frame = CGRectMake(0, BUTTONMarginUP, NAVBUTTON_WIDTH, NAVBUTTON_HEIGHT);
    tmpLeftButton.showsTouchWhenHighlighted = NO;
    tmpLeftButton.exclusiveTouch = YES;
    
    if (bgImageName != nil)//设置button的背景
    {
        [tmpLeftButton setBackgroundImage:[UIImage imageNamed:bgImageName] forState:UIControlStateNormal];
    }

    [tmpLeftButton setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [tmpLeftButton setImage:[UIImage imageNamed:@"icon-3"] forState:UIControlStateHighlighted];

    [tmpLeftButton addTarget:self action:@selector(leftButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    
    self.leftButton = tmpLeftButton;
    UIBarButtonItem *leftButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tmpLeftButton];
    
    if (SYSTEM_VERSION >= 7.0)//左边button的偏移量，从左移动13个像素
    {
        UIBarButtonItem *negativeSeperator = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
        negativeSeperator.width = -10;
        [self.navigationItem setLeftBarButtonItems:@[negativeSeperator, leftButtonItem]];
    }
    else
    {
        [self.navigationItem setLeftBarButtonItem:leftButtonItem];
    }
}

- (void)setLeftButtonWithImageName:(NSString*)imageName
					   bgImageName:(NSString*)bgImageName
				tipButtonImageName:(NSString*)tipImageName
				  tipBackImageName:(NSString*)tipBackImageName
{
	//leftButton
	UIImage* leftImage = [UIImage imageNamed:imageName];
	CGFloat leftWidth = leftImage.size.width;
	CGFloat leftHeight = leftImage.size.height;
	UIButton* tmpLeftButton = [UIButton buttonWithType:UIButtonTypeCustom];
	tmpLeftButton.frame = CGRectMake(0, BUTTONMarginUP, NAVBUTTON_WIDTH, NAVBUTTON_HEIGHT);
	tmpLeftButton.showsTouchWhenHighlighted = NO;
	tmpLeftButton.exclusiveTouch = YES;
	[tmpLeftButton setImage:leftImage forState:UIControlStateNormal];
	[tmpLeftButton setImage:[UIImage imageNamed:@"icon-3"] forState:UIControlStateHighlighted];
	[tmpLeftButton addTarget:self action:@selector(leftButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
	self.leftButton = tmpLeftButton;
//	UIBarButtonItem* leftButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tmpLeftButton];
	
	//tipButton
	UIImage* tipImage = [UIImage imageNamed:tipImageName];
	CGFloat width = tipImage.size.width;
	UIButton* tipButton = [UIButton buttonWithType:UIButtonTypeCustom];
	tipButton.frame = CGRectMake(leftWidth + 3,
								 NAVBUTTON_HEIGHT - leftHeight - 4,
								 width,
								 width);
	tipButton.showsTouchWhenHighlighted = NO;
	tipButton.exclusiveTouch = YES;
	[tipButton setImage:tipImage forState:UIControlStateNormal];
	[tipButton setImage:tipImage forState:UIControlStateHighlighted];
	//	UIBarButtonItem* tipButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tipButton];
	
	UIView* backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, NAVBUTTON_WIDTH, NAVBUTTON_HEIGHT)];
	[backView addSubview:tmpLeftButton];
	[backView addSubview:tipButton];
	
	UIBarButtonItem* tipItem = [[UIBarButtonItem alloc] initWithCustomView:backView];
	
	if (SYSTEM_VERSION >= 7.0)
	{
		UIBarButtonItem *negativeSeperator = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
																						   target:nil
																						   action:nil];
		negativeSeperator.width = -10;
		[self.navigationItem setLeftBarButtonItems:@[negativeSeperator, tipItem]];
	}
	else
	{
		[self.navigationItem setLeftBarButtonItem:tipItem];
	}
}

- (void)setRightButton:(NSString *)title
      normalTitleColor:(UIColor *)normalTitleColor
 highlightedTitleColor:(UIColor *)highlightedTitleColor
    disabledTitleColor:(UIColor *)disabledTitleColor
           bgImageName:(NSString *)bgImageName;
{
    UIButton *tmpRightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    
    tmpRightButton.frame = CGRectMake(0, BUTTONMarginUP, NAVBUTTON_WIDTH, NAVBUTTON_HEIGHT);
    tmpRightButton.showsTouchWhenHighlighted = NO;
    tmpRightButton.exclusiveTouch = YES;

    [tmpRightButton setBackgroundImage:[UIImage imageNamed:bgImageName] forState:UIControlStateNormal];
    [tmpRightButton setTitleColor:normalTitleColor forState:UIControlStateNormal];
    [tmpRightButton setTitleColor:normalTitleColor forState:UIControlStateHighlighted];
    [tmpRightButton setTitleColor:normalTitleColor forState:UIControlStateDisabled];

    [tmpRightButton addTarget:self action:@selector(rightButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    
    self.rightButton = tmpRightButton;
    UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tmpRightButton];
    
    if (SYSTEM_VERSION >= 7.0)//左边button的偏移量，从左移动13个像素
    {
        UIBarButtonItem *negativeSeperator = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
        negativeSeperator.width = -10;
        [self.navigationItem setRightBarButtonItems:@[negativeSeperator, rightButtonItem]];
    }
    else
    {
        [self.navigationItem setRightBarButtonItem:rightButtonItem];
    }
}

- (void)setRightButtonWithHide:(BOOL)hide
{
    self.rightButton.hidden = hide;
}

-(void)setLeftButtonWithHide:(BOOL)hide{
    self.leftButton.hidden = hide;
}

//设置导航右边的button的图片名和背景图片名
- (void)setRightButtonWithImageName:(NSString *)imageName bgImageName:(NSString *)bgImageName
{
    UIButton *tmpRightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    tmpRightButton.frame = CGRectMake(self.view.frame.size.width-NAVBUTTON_WIDTH-BUTTONMarginX, BUTTONMarginUP, NAVBUTTON_WIDTH, NAVBUTTON_HEIGHT);
    
    tmpRightButton.showsTouchWhenHighlighted = NO;
    tmpRightButton.exclusiveTouch = YES;
    
    if (bgImageName && bgImageName.length>0)//设置button的背景
    {
        [tmpRightButton setBackgroundImage:[UIImage imageNamed:bgImageName] forState:UIControlStateNormal];
    }
    
    [tmpRightButton setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [tmpRightButton addTarget:self action:@selector(rightButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    self.rightButton = tmpRightButton;
    UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tmpRightButton];
    
    if (SYSTEM_VERSION >= 7.0)//左边button的偏移量，从左移动13个像素
    {
        UIBarButtonItem *negativeSeperator = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
        negativeSeperator.width = -10;
        [self.navigationItem setRightBarButtonItems:@[negativeSeperator, rightButtonItem]];
    }
    else
    {
        [self.navigationItem setRightBarButtonItem:rightButtonItem];
    }
}

- (void)setRightButtonWithImageName:(NSString *)imageName pressedImageName:(NSString *)pressedImageName{
    
    UIButton *tmpRightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    tmpRightButton.frame = CGRectMake(self.view.frame.size.width-NAVBUTTON_WIDTH-BUTTONMarginX, BUTTONMarginUP, NAVBUTTON_WIDTH, NAVBUTTON_HEIGHT);
    
    tmpRightButton.showsTouchWhenHighlighted = NO;
    tmpRightButton.exclusiveTouch = YES;
    
    [tmpRightButton setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [tmpRightButton setImage:[UIImage imageNamed:pressedImageName] forState:UIControlStateHighlighted];
    
    [tmpRightButton addTarget:self action:@selector(rightButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    self.rightButton = tmpRightButton;
    UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tmpRightButton];
    
    if (SYSTEM_VERSION >= 7.0)//左边button的偏移量，从左移动13个像素
    {
        UIBarButtonItem *negativeSeperator = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
        negativeSeperator.width = -10;
        [self.navigationItem setRightBarButtonItems:@[negativeSeperator, rightButtonItem]];
    }
    else
    {
        [self.navigationItem setRightBarButtonItem:rightButtonItem];
    }
}

//设置导航右边的button的图片名和背景图片名
- (void)setRightCallButtonWithImageName:(NSString *)callImageName
                   callPressedImageName:(NSString *)callPressedImageName
                   rightButtonImageName:(NSString *)rightImageName
                  rightPressedImageName:(NSString *)rightPressedImageName
{
    UIImage *rigthImg = [UIImage imageNamed:rightImageName];
    UIImage *callImg = [UIImage imageNamed:callImageName];
    
    CGFloat imageWidth = rigthImg.size.width;
    
    UIButton *tmpRightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    tmpRightButton.frame = CGRectMake(self.view.frame.size.width-imageWidth-BUTTONMarginX, BUTTONMarginUP, imageWidth, NAVBUTTON_HEIGHT);
    tmpRightButton.showsTouchWhenHighlighted = NO;
    tmpRightButton.exclusiveTouch = YES;
    [tmpRightButton setImage:rigthImg forState:UIControlStateNormal];
    [tmpRightButton setImage:[UIImage imageNamed:rightPressedImageName] forState:UIControlStateHighlighted];
    [tmpRightButton addTarget:self action:@selector(rightButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    self.rightButton = tmpRightButton;
    
    
    UIButton *tmpCallButton = [UIButton buttonWithType:UIButtonTypeCustom];
    tmpCallButton.frame = CGRectMake(self.view.frame.size.width-imageWidth*2-BUTTONMarginX*2, BUTTONMarginUP, imageWidth, NAVBUTTON_HEIGHT);
    
    tmpCallButton.showsTouchWhenHighlighted = NO;
    tmpCallButton.exclusiveTouch = YES;
    [tmpCallButton setImage:callImg forState:UIControlStateNormal];
    [tmpCallButton setImage:[UIImage imageNamed:callPressedImageName] forState:UIControlStateHighlighted];
    [tmpCallButton addTarget:self action:@selector(callButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    self.callButton = tmpCallButton;
    
    UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tmpRightButton];
    UIBarButtonItem *callButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tmpCallButton];
    
    if (SYSTEM_VERSION >= 7.0)//左边button的偏移量，从左移动13个像素
    {
        UIBarButtonItem *negativeSeperatortmp = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
        negativeSeperatortmp.width = 20;

        [self.navigationItem setRightBarButtonItems:@[rightButtonItem,negativeSeperatortmp,callButtonItem]];
    }
    else
    {
        [self.navigationItem setRightBarButtonItems:@[rightButtonItem,callButtonItem]];
    }
}

//设置导航可以透明
- (void)setIsTransparent:(BOOL)isTransparent
{
    if (isTransparent)//透明
    {
        [self clearNavigationBar];
    }
    else//不透明
    {
        [self setNavigationBarImage];
    }
}

//导航设置为透明
- (void)clearNavigationBar
{
//    if (SYSTEM_VERSION >=7.0) {
//        self.navigationController.view.backgroundColor = [UIColor whiteColor];
//        [self.navigationController.navigationBar setBackgroundImage:[UIImage createImageWithColor:[UIColor clearColor] andSize:CGSizeMake(320, 64)] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
//    }
//    else
//    {
//        self.navigationController.view.backgroundColor = [UIColor whiteColor];
//        [self.navigationController.navigationBar setBackgroundImage:[UIImage createImageWithColor:[UIColor clearColor] andSize:CGSizeMake(320, 44)] forBarMetrics:UIBarMetricsDefault];
//    }
//    
//    if ([UINavigationBar instancesRespondToSelector:@selector(setShadowImage:)])
//    {
//        [self.navigationController.navigationBar setShadowImage:[UIImage createImageWithColor:[UIColor clearColor] andSize:CGSizeMake(320, 3)]];
//    }
}

//设置导航图片
- (void)setNavigationBarImage
{
    if (SYSTEM_VERSION >=7.0)
    {
        UIImage *syncBgImg = [UIImage imageNamed:@"navBg128_64.png"];
        [self.navigationController.navigationBar setBackgroundImage:syncBgImg forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
    }
    else
    {
        UIImage *syncBgImg =  [UIImage imageNamed:@"navBg128_64.png"];
        [self.navigationController.navigationBar setBackgroundImage:syncBgImg forBarMetrics:UIBarMetricsDefault];
    }
    
//    if ([UINavigationBar instancesRespondToSelector:@selector(setShadowImage:)])
//    {
//        [self.navigationController.navigationBar setShadowImage:[UIImage imageNamed:@"nav_public_titlebar_bg02"]];
//    }
}


- (void)createLeftAndRightMenuItem:(UIView *(^)(KNaviItemIndex nIndex))menuItem
{
    UIView *leftButton = menuItem(KLeftButtonItem);
    if (leftButton != nil)
    {
        UIBarButtonItem *leftButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];

        if (SYSTEM_VERSION >= 7.0)//左边button的偏移量，从左移动13个像素
        {
            UIBarButtonItem *negativeSeperator = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
            negativeSeperator.width = -10;
            [self.navigationItem setLeftBarButtonItems:@[negativeSeperator, leftButtonItem]];
        }
        else
        {
            [self.navigationItem setLeftBarButtonItem:leftButtonItem];
        }
        self.leftButton = (UIButton*)leftButton;
    }
    
    UIView *rightButton = menuItem(KRightButtonItem);
    if (rightButton != nil)
    {
        UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];

        if (SYSTEM_VERSION >= 7.0)//左边button的偏移量，从左移动13个像素
        {
            UIBarButtonItem *negativeSeperator = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
            negativeSeperator.width = -10;
            [self.navigationItem setRightBarButtonItems:@[negativeSeperator, rightButtonItem]];
        }
        else
        {
            [self.navigationItem setRightBarButtonItem:rightButtonItem];
        }
        self.rightButton = (UIButton*)rightButton;
    }
}

- (void)createLeftAndRightMenuItem:(UIView *(^)())leftItem right:(UIView *(^)())rightItem
{
    UIView * leftButton;
    
    if (leftItem)
    {
       leftButton = leftItem();
    }
    
    if (leftButton != nil)
    {
        UIBarButtonItem *leftButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
        
        if (SYSTEM_VERSION >= 7.0)//左边button的偏移量，从左移动13个像素
        {
            UIBarButtonItem *negativeSeperator = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
            negativeSeperator.width = -10;
            [self.navigationItem setLeftBarButtonItems:@[negativeSeperator, leftButtonItem]];
        }
        else
        {
            [self.navigationItem setLeftBarButtonItem:leftButtonItem];
        }
    }
    
    UIView * rightButton;
    
    if (rightItem)
    {
        rightButton = rightItem();
        
    }
    
    if (rightButton != nil)
    {
        UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
        
        if (SYSTEM_VERSION >= 7.0)//左边button的偏移量，从左移动13个像素
        {
            UIBarButtonItem *negativeSeperator = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
            negativeSeperator.width = -10;
            [self.navigationItem setRightBarButtonItems:@[negativeSeperator, rightButtonItem]];
        }
        else
        {
            [self.navigationItem setRightBarButtonItem:rightButtonItem];
        }
    }
}

- (void)createTitleButtonWithCount:(NSInteger)buttonCount titleArray:(NSArray*)titleArray action:(void (^)(KNaviItemIndex nIndex))action
{
    if (buttonCount <= 0)
    {
        return;
    }
    
    int btnWidth = 80;
    
    UIImage *groupImage = [UIImage imageNamed:@"friends_titlebar_button_left_on"];
    CGRect rect = CGRectMake(0, 0, btnWidth*buttonCount, 40);
    UIView* navTitleView = [[UIView alloc]initWithFrame:rect];
    CGRect rectBtn = CGRectMake(0, 6, btnWidth, groupImage.size.height);
    navTitleView.clipsToBounds = YES;
    KNaviItemIndex actionIndex = KLeftButtonItem;
    for (int i=KLeftButtonItem; i<buttonCount; i++)
    {
        NSString* title=nil;
        
        if (i < titleArray.count)
        {
            title = [titleArray objectAtIndex:i];
        }
        
        UIButton* button =nil;
        
        if (i == KLeftButtonItem)
        {
            button = [self buttonWithTitle: title
                          stateNormalImage: @"friends_titlebar_button_left_normal"
                        stateSelectedImage: @"friends_titlebar_button_left_on"
                                   postion: rectBtn associatedBlock:^{
                if (action)
                {
                    action(actionIndex);
                }
            }];
            actionIndex++;
            
            [navTitleView addSubview:button];
            continue;
        }
        if (i == KRightButtonItem)
        {
            rectBtn.origin.x = btnWidth*i;
            button = [self buttonWithTitle:title
                          stateNormalImage: @"friends_titlebar_button_right_normal"
                        stateSelectedImage: @"friends_titlebar_button_right_on"
                                   postion: rectBtn associatedBlock:^{
                if (action)
                {
                    action(actionIndex);
                }
            }];
            actionIndex++;
            
            [navTitleView addSubview:button];
            continue;
        }
        rectBtn.origin.x = btnWidth*i;
        button = [self buttonWithTitle: title
                      stateNormalImage: @"friends_titlebar_button_right_normal"
                    stateSelectedImage: @"friends_titlebar_button_right_on"
                               postion:rectBtn associatedBlock:^{
            if (action)
            {
                action(actionIndex);
            }
        }];
        actionIndex++;
        
        [navTitleView addSubview:button];
    }
    self.navigationItem.titleView = navTitleView;
}

//创建button
static const char * const RCSTitleButtonBlockKey = "RCSTitleViewButtonBlock";

- (UIButton *)buttonWithTitle:(NSString *)aTitle
             stateNormalImage:(NSString*)stateNormalImage
            stateSelectedImage:(NSString*)stateSelectedImage
                      postion:(CGRect)postion
              associatedBlock:(RCSBaseTitleButtonBlock)block
{
    UIButton *button = [[UIButton alloc] initWithFrame:postion];
    [button setTitle:aTitle forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//    [button setTitleColor:[UIImage tintColor:0] forState:UIControlStateNormal];
    [button.titleLabel setFont:[UIFont systemFontOfSize:12]];
    [button setBackgroundImage:[UIImage imageNamed:stateNormalImage] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:stateSelectedImage] forState:UIControlStateSelected];
    [button addTarget:self action:@selector(buttonWasPressed:) forControlEvents:UIControlEventTouchUpInside];
    button.showsTouchWhenHighlighted = NO;
    button.selected = NO;
    //objc_setAssociatedObject(button, RCSTitleButtonBlockKey, block, OBJC_ASSOCIATION_RETAIN);
    return button;
}

//button 事件处理
- (void)buttonWasPressed:(UIButton *)sender
{
//    RCSBaseTitleButtonBlock block = objc_getAssociatedObject(sender, RCSTitleButtonBlockKey);
//    if(sender.isSelected){
//        sender.selected = NO;
//    }
//    else{
//        sender.selected = YES;
//    }
//    if(block) block();
}

- (void)reloadSkinImage
{
    if (SYSTEM_VERSION >=7.0) {
        UIImage *syncBgImg = [UIImage imageNamed:@"navBg128_64.png"];
        [self.navigationController.navigationBar setBackgroundImage:syncBgImg forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
    }
    else
    {
        UIImage *syncBgImg =  [UIImage imageNamed:@"navBg128_64.png"];
        [self.navigationController.navigationBar setBackgroundImage:syncBgImg forBarMetrics:UIBarMetricsDefault];
    }
}

- (void)leftButtonPressed:(id)action
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)rightButtonPressed:(id)action
{
    //vLOG(@"子类需要重写右按钮方法");
}

/**
 *	@brief	拨打界面按钮
 *
 *	@param 	action
 *
 *	@return	N/A
 */
- (void)callButtonPressed:(id)action
{
  //  vLOG(@"子类需要重写右按钮方法");
}

//-(void)UICallbackWithType:(RcSLogic2UiCmd)type state:(CmdReqState)state object:(NSDictionary*)param
//{
//    
//}

#pragma mark-旋屏的方法设置

//返回该controller支持的设备状态
- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

//设置该controller是否可自动旋屏
- (BOOL)shouldAutorotate
{
    return NO;
}

//为了兼容IOS6以前的版本而保留的方法
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
}

//当发送旋屏事件后，就会掉用该方法
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration NS_AVAILABLE_IOS(3_0)
{
    
}

- (NSString *)getImagePathFromMapView:(UIView *)view imageName:(NSString*)imageName
{
//    CGSize mapSize = CGSizeMake(self.view.frame.size.width, 49);
//    
//    UIImage* image = [self getImageFromView:view size:mapSize];
//    NSData* imageData = UIImageJPEGRepresentation(image, 1.0);
//    
//    NSString* imageRelativePath = nil;
//    if (imageName)
//    {
//        imageRelativePath = imageName;
//    }
//    else
//    {
//        imageRelativePath = [NSString stringWithUUID];
//    }
//    NSString* imagePath = [NSString stringWithFormat:@"%@",[kRcSFileUtil getShareContentImageFilePath:imageRelativePath]];
//    if ([imageData writeToFile:imagePath atomically:YES]) {
//        return imageRelativePath;
//    }
    return nil;
}

- (UIImage *)getImageFromView:(UIView *)view size:(CGSize)size
{
    if (&UIGraphicsBeginImageContextWithOptions != NULL)
    {
        UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
    }
    else
    {
        UIGraphicsBeginImageContext(size);
    }
    
    //获取图像
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}

- (NSString *)tabBarCaptureImage
{
    [self removeTabBarImage];
    return[self getImagePathFromMapView:self.navigationController.tabBarController.tabBar imageName:KTabBarImageName];
}

- (void)removeTabBarImage
{
    //[kFileUtil deleteFileWithPath:[self getTabBarImagePath]];
}

- (NSString *)getTabBarImagePath
{
   // return [NSString stringWithFormat:@"%@",[kRcSFileUtil getShareContentImageFilePath:KTabBarImageName]];
    return nil;
}

- (BOOL)canDragBack
{
    return YES;
}


- (void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];
}


- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
}

- (void) removeControllerFromStack:(Class)c
{
    NSMutableArray *navigationStack = [[NSMutableArray alloc] initWithArray:
                                       self.navigationController.viewControllers];
    for (UIViewController *controller in navigationStack)
    {
        if ([controller isKindOfClass:c])
        {
            [navigationStack removeObject:controller];
            break;
        }
    }
    self.navigationController.viewControllers = navigationStack;
}

////判断网络连接状态
//-(BOOL)showAlwer
//{
//    id <IRcSLogin> loginModel =[[RcSModuleManager getInferfaceManager]getIRcSLogin];
//    
//    if ([loginModel getNetType] == AFNetworkReachabilityStatusNotReachable) {
//        
//        dispatch_async(dispatch_get_main_queue(), ^{
//            
//            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"网络断开，请检查网络" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
//            [alert show];
//            
//            [alert performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:YES];
//        });
//    }
//    return !([loginModel getNetType] == AFNetworkReachabilityStatusNotReachable);
//}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
